package org.jeecg.modules.htxincailiao.ability.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilityCategory;
import org.jeecg.modules.htxincailiao.ability.entity.HTAbilityDemand;
import org.jeecg.modules.htxincailiao.ability.service.IHTAbilityCategoryService;
import org.jeecg.modules.htxincailiao.ability.service.IHTAbilityDemandService;
import org.jeecg.modules.htxincailiao.account.entity.HTMerchant;
import org.jeecg.modules.htxincailiao.account.service.IHTMerchantService;
import org.jeecg.modules.htxincailiao.address.domain.vo.HTAddressVO;
import org.jeecg.modules.htxincailiao.address.service.IHTShopAddressService;
import org.jeecg.modules.htxincailiao.shop.entity.HTShop;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.jeecg.modules.permissions.HTRolePermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: ht_ability_demand
 * @Author: jeecg-boot
 * @Date: 2021-07-05
 * @Version: V1.0
 */
@Api(tags = "ht_ability_demand")
@RestController
@RequestMapping("/ability/hTAbilityDemand")
@Slf4j
public class HTAbilityDemandController extends JeecgController<HTAbilityDemand, IHTAbilityDemandService> {
    @Resource
    private IHTAbilityDemandService hTAbilityDemandService;

    @Resource
    private IHTShopAddressService shopAddressService;
    @Resource
    private IHTAbilityCategoryService abilityCategoryService;

    @Resource
    private IHTShopService shopService;

    @Resource
    private IHTMerchantService merchantService;

    /**
     * 分页列表查询
     *
     * @param hTAbilityDemand
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_ability_demand-分页列表查询")
    @ApiOperation(value = "ht_ability_demand-分页列表查询", notes = "ht_ability_demand-分页列表查询")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPageList(HTAbilityDemand hTAbilityDemand,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTAbilityDemand = JSONObject.parseObject(line, HTAbilityDemand.class);

                pageNo = oConvertUtils.isNotEmpty(hTAbilityDemand.getPageNo()) ? hTAbilityDemand.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTAbilityDemand.getPageSize()) ? hTAbilityDemand.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTAbilityDemand) &&
                oConvertUtils.isNotEmpty(hTAbilityDemand.getCategoryId()) &&
                hTAbilityDemand.getCategoryId().equals("0")) {
            //全部分类
            hTAbilityDemand.setCategoryId(null);
        }

        HTRolePermission.AddAbilityDemandPermission(req, hTAbilityDemand);

        QueryWrapper<HTAbilityDemand> queryWrapper = QueryGenerator.initQueryWrapper(hTAbilityDemand, req.getParameterMap());
        queryWrapper.orderByDesc("update_time");
        Page<HTAbilityDemand> page = new Page<HTAbilityDemand>(pageNo, pageSize);
        IPage<HTAbilityDemand> pageList = hTAbilityDemandService.page(page, queryWrapper);
        List<HTAbilityDemand> records = pageList.getRecords();
        records.forEach(demand -> {

            HTAbilityCategory category = abilityCategoryService.getById(demand.getCategoryId());
            if (oConvertUtils.isNotEmpty(category)) {
                demand.setCategoryName(category.getName());
            }

            HTMerchant htMerchant = merchantService.getById(demand.getShopId());
            if (oConvertUtils.isNotEmpty(htMerchant)) {
                demand.setMerchantInfo(htMerchant);
            }

        });

        return Result.OK(pageList);
    }
    /**
     * 分页列表查询
     *
     * @param hTAbilityDemand
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_ability_demand-分页列表查询")
    @ApiOperation(value = "ht_ability_demand-分页列表查询", notes = "ht_ability_demand-分页列表查询")
    @RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPCHomePageList(HTAbilityDemand hTAbilityDemand,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTAbilityDemand = JSONObject.parseObject(line, HTAbilityDemand.class);

                pageNo = oConvertUtils.isNotEmpty(hTAbilityDemand.getPageNo()) ? hTAbilityDemand.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTAbilityDemand.getPageSize()) ? hTAbilityDemand.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTAbilityDemand) &&
                oConvertUtils.isNotEmpty(hTAbilityDemand.getCategoryId()) &&
                hTAbilityDemand.getCategoryId().equals("0")) {
            //全部分类
            hTAbilityDemand.setCategoryId(null);
        }

        HTRolePermission.AddAbilityDemandPermission(req, hTAbilityDemand);

        QueryWrapper<HTAbilityDemand> queryWrapper = QueryGenerator.initQueryWrapper(hTAbilityDemand, req.getParameterMap());
        queryWrapper.orderByDesc("is_home_recommended").orderByDesc("update_time");

        Page<HTAbilityDemand> page = new Page<HTAbilityDemand>(pageNo, pageSize);
        IPage<HTAbilityDemand> pageList = hTAbilityDemandService.page(page, queryWrapper);
        List<HTAbilityDemand> records = pageList.getRecords();
        records.forEach(demand -> {

            HTAbilityCategory category = abilityCategoryService.getById(demand.getCategoryId());
            if (oConvertUtils.isNotEmpty(category)) {
                demand.setCategoryName(category.getName());
            }

            HTMerchant merchant = merchantService.getById(demand.getShopId());
            if (oConvertUtils.isNotEmpty(merchant)) {
                demand.setMerchantInfo(merchant);
            }

        });

        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTAbilityDemand
     * @return
     */
    @AutoLog(value = "ht_ability_demand-添加")
    @ApiOperation(value = "ht_ability_demand-添加", notes = "ht_ability_demand-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTAbilityDemand hTAbilityDemand) {
        hTAbilityDemandService.save(hTAbilityDemand);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTAbilityDemand
     * @return
     */
    @AutoLog(value = "ht_ability_demand-编辑")
    @ApiOperation(value = "ht_ability_demand-编辑", notes = "ht_ability_demand-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTAbilityDemand hTAbilityDemand) {
        hTAbilityDemandService.updateById(hTAbilityDemand);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_ability_demand-通过id删除")
    @ApiOperation(value = "ht_ability_demand-通过id删除", notes = "ht_ability_demand-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTAbilityDemandService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_ability_demand-批量删除")
    @ApiOperation(value = "ht_ability_demand-批量删除", notes = "ht_ability_demand-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTAbilityDemandService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_ability_demand-通过id查询")
    @ApiOperation(value = "ht_ability_demand-通过id查询", notes = "ht_ability_demand-通过id查询")
    @RequestMapping(value = "/queryById", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryById(HttpServletRequest req, @RequestParam(name = "id", required = false) String id) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                HTAbilityDemand hTAbilityDemand = JSONObject.parseObject(line, HTAbilityDemand.class);
                id = hTAbilityDemand.getId();
            }
        }

        HTAbilityDemand hTAbilityDemand = hTAbilityDemandService.getById(id);
        if (oConvertUtils.isNotEmpty(hTAbilityDemand) && oConvertUtils.isNotEmpty(hTAbilityDemand.getCategoryId())) {
            HTAbilityCategory category = abilityCategoryService.getById(hTAbilityDemand.getCategoryId());
            if (oConvertUtils.isNotEmpty(category) && oConvertUtils.isNotEmpty(category.getName())) {
                hTAbilityDemand.setCategoryName(category.getName());
            }
        }

        if (hTAbilityDemand == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTAbilityDemand);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTAbilityDemand
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTAbilityDemand hTAbilityDemand) {
        return super.exportXls(request, hTAbilityDemand, HTAbilityDemand.class, "ht_ability_demand");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTAbilityDemand.class);
    }

}
